﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class Message_MessageManager : System.Web.UI.Page
{
    List<Message> list = new MessageManager().GetAllMessage();
    public static string java=null;
    protected void Page_Load(object sender, EventArgs e)
    {
       //this.GridView1.Attributes.Add("onclick", java);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
       
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Attributes.Add("onclick", "javascript:OpenContent(300,300,"+GridView1.DataKeys[e.Row.RowIndex].Value+")");
           
            int id = Convert.ToInt32(e.Row.Cells[4].Text);
            List<MessageToUser> useLlist = new MsgToUserManager().GetAllMsgToUserByMsgId(id);

            if (useLlist.Count == 1)
            {
                Label lable = new Label();
                lable.Text = useLlist[0].ToUserId;
                e.Row.Cells[4].Controls.Add(lable);
            }
            if (useLlist.Count > 1)
            {
                e.Row.Cells[4].Attributes.Add("onclick", "javascript:OpenUser(300,300," + GridView1.DataKeys[e.Row.RowIndex].Value + ")");
                LinkButton lb = new LinkButton();
                 lb.Text = "查看详单";
                e.Row.Cells[4].Controls.Add(lb);
            }
            if (useLlist.Count==0)
            {
                 Label lable = new Label();
                 lable.Text = "";
                e.Row.Cells[4].Controls.Add(lable);
            }

            int num = Convert.ToInt32(e.Row.Cells[10].Text);
           LinkButton l = new LinkButton();
            l.Text = "发布";
            l.CommandName = "publish";
            if (num == 1)
            {
                l.Enabled = false;
                ((ImageButton)(e.Row.Cells[8].FindControl("imgModify"))).Enabled = false;
            }
            else
            {
                l.Enabled = true;
                ((ImageButton)(e.Row.Cells[8].FindControl("imgModify"))).Enabled = true;
            }
            e.Row.Cells[10].Controls.Add(l);
            
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName=="modify")
        {
            GridViewRow dv = (GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent);
            int msgId =Convert.ToInt32(GridView1.DataKeys[dv.RowIndex].Value);
            Response.Redirect(string.Format("AddMessage.aspx?mid={0}",msgId));
        }
       if (e.CommandName=="publish")
        {
            GridViewRow dv = (GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent);
            int msgId = Convert.ToInt32(GridView1.DataKeys[dv.RowIndex].Value);
            Message msg = new MessageManager().GetMessageById(msgId);
            msg.IfPublish = 1;
            new MessageManager().UpdateMessage(msg);
            GridView1.DataSource = new MessageManager().GetAllMessage();
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int msgId =Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
    }
}